<?php

/**
 * 短信息
 */
class Application_Model_Sms extends Application_Model_DomainObject{

    protected $_id;         //id
    protected $_type;       //1注册
    protected $_phone;      //手机号(加区号)
    protected $_message;    //消息内容
    protected $_key;        //验证码
    protected $_state;      //1未用(可能过期)2已用(可能批量更新)
    protected $_ip;         //ip
    protected $_result;     //服务商返回
    protected $_time;       //发送时间
    protected $_endtime;    //发送成功后过期时间

    private static $key    = '340b4399';
    private static $secret = 'c4552599';

    /**
     * 获取数据
     * @return list
    */
    public static function getList($phone = '', $type = 1, $pagesize = 20, $pageindex = 0)
    {
        $where = array();
        $and = "";
        if($phone != "") {
            $where[" phone like "] = "%".$phone."%";
            $and = " and ";
        }
        if($type !== "") {
            $where[$and." type = "] = $type;
            $and = " and ";
        }
        return self::getDao()->queryPage($where, " id desc", $pagesize, $pageindex);
    }

    /**
     * 获取账户余额
    */
    public static function balance()
    {
        try {
            $url  = 'https://rest.nexmo.com/account/get-balance/'. self::$key .'/'. self::$secret;
            $ch   = curl_init();
            $head = array(
                        'Accept: application/json',
                    );
            curl_setopt($ch, CURLOPT_HTTPHEADER,     $head);
            curl_setopt($ch, CURLOPT_URL,            $url);
            curl_setopt($ch, CURLOPT_HEADER,         0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            $re = curl_exec($ch);
            if($re !== false){
                $re = json_decode($re, true);
                $re = $re['value'];
            }
        } catch (Exception $e) {
            $re = false;
        } finally {
            curl_close($ch);
        }
        return $re;
    }
}